/**
 * output package name
 */
package com.kingdee.eas.custom.hr.attendance.client;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.ResourceBundle;

import javax.swing.UIManager;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.ctrl.extendcontrols.KDBizPromptBox;
import com.kingdee.bos.ctrl.kdf.table.ICell;
import com.kingdee.bos.ctrl.kdf.table.IRow;
import com.kingdee.bos.ctrl.kdf.table.KDTDefaultCellEditor;
import com.kingdee.bos.ctrl.kdf.table.KDTSelectBlock;
import com.kingdee.bos.ctrl.swing.KDDatePicker;
import com.kingdee.bos.ctrl.swing.KDLayout;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.data.SortType;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.bos.metadata.entity.SorterItemInfo;
import com.kingdee.bos.ui.face.CoreUIObject;
import com.kingdee.bos.ui.face.IUIWindow;
import com.kingdee.bos.ui.face.UIFactory;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.common.client.UIContext;
import com.kingdee.eas.common.client.UIFactoryName;
import com.kingdee.eas.custom.hr.attendance.AttendanceTimeConfigCollection;
import com.kingdee.eas.custom.hr.attendance.AttendanceTimeConfigException;
import com.kingdee.eas.custom.hr.attendance.AttendanceTimeConfigFactory;
import com.kingdee.eas.custom.hr.attendance.AttendanceTimeConfigInfo;
import com.kingdee.eas.custom.hr.attendance.IAttendanceTimeConfig;
import com.kingdee.eas.framework.client.multiDetail.DetailPanel;
import com.kingdee.eas.framework.client.multiDetail.HMDUtils;
import com.kingdee.eas.hr.compensation.CmpPeriodCollection;
import com.kingdee.eas.hr.compensation.CmpPeriodFactory;
import com.kingdee.eas.hr.compensation.CmpPeriodInfo;
import com.kingdee.eas.hr.compensation.util.HRSchemeUtils;
import com.kingdee.eas.hr.compensation.util.HRUtils;
import com.kingdee.eas.util.SysUtil;
import com.kingdee.eas.util.client.EASResource;
import com.kingdee.eas.util.client.MsgBox;
import com.kingdee.util.DateTimeUtils;

/**
 * output class name
 */
public class AttendanceTimeConfigEditUI extends
		AbstractAttendanceTimeConfigEditUI {
	private static final Logger logger = CoreUIObject
			.getLogger(AttendanceTimeConfigEditUI.class);

	public static final Color REQUIREDBACKGROUND = UIManager
			.getColor("TextField.requiredBackground");

	private Color notEditCellBGColor = new Color(217, 217, 210);

	public static final ResourceBundle resouce = ResourceBundle
			.getBundle("com.kingdee.eas.custom.hr.attendance.Config");

	protected DetailPanel tblMain_detailPanel = null;

	private AttendanceTimeConfigCollection infoCollection = new AttendanceTimeConfigCollection();
	private HROrgUnitInfo hrOrgUnitInfo = null;

	private IRow row = null;
	private Hashtable hstCode = new Hashtable();
	private HashSet hstDeleteSet = new HashSet();
	private int rowID = 0;
	private boolean isChang = false;

	/**
	 * output class constructor
	 */
	public AttendanceTimeConfigEditUI() throws Exception {
		super();
		this.tblMain.checkParsed();

		KDDatePicker beginDate_DatePicker = new KDDatePicker();
		beginDate_DatePicker.setName("beginDate_DatePicker");
		beginDate_DatePicker.setVisible(true);
		beginDate_DatePicker.setEditable(true);
		KDTDefaultCellEditor beginDate_CellEditor = new KDTDefaultCellEditor(
				beginDate_DatePicker);
		this.tblMain.getColumn("beginDate").setEditor(beginDate_CellEditor);

		KDDatePicker endDate_DatePicker = new KDDatePicker();
		endDate_DatePicker.setName("endDate_DatePicker");
		endDate_DatePicker.setVisible(true);
		endDate_DatePicker.setEditable(true);
		KDTDefaultCellEditor endDate_CellEditor = new KDTDefaultCellEditor(
				endDate_DatePicker);
		this.tblMain.getColumn("endDate").setEditor(endDate_CellEditor);

		final KDBizPromptBox period_PromptBox = new KDBizPromptBox();
		period_PromptBox
				.setQueryInfo("com.kingdee.eas.hr.compensation.app.CmpPeriodQuery");
		period_PromptBox.setVisible(true);
		period_PromptBox.setEditable(true);
		period_PromptBox.setDisplayFormat("$name$");
		period_PromptBox.setEditFormat("$number$");
		period_PromptBox.setCommitFormat("$number$");
		KDTDefaultCellEditor period_CellEditor = new KDTDefaultCellEditor(
				period_PromptBox);
		this.tblMain.getColumn("period").setEditor(period_CellEditor);
	}

	@Override
	public void initUIContentLayout() {
		super.initUIContentLayout();
		tblMain_detailPanel = (DetailPanel) HMDUtils.buildDetail(this,
				dataBinder, tblMain, new AttendanceTimeConfigInfo(), null,
				false);
		this.add(tblMain_detailPanel, new KDLayout.Constraints(14, 14, 983,
				597, KDLayout.Constraints.ANCHOR_TOP
						| KDLayout.Constraints.ANCHOR_LEFT
						| KDLayout.Constraints.ANCHOR_RIGHT_SCALE));
		btnNaturalMonth.setIcon(EASResource.getIcon("imgTbtn_setting"));
	}

	/**
	 * output storeFields method
	 */
	public void storeFields() {
		super.storeFields();
	}

	/**
	 * output actionPageSetup_actionPerformed
	 */
	public void actionPageSetup_actionPerformed(ActionEvent e) throws Exception {
		super.actionPageSetup_actionPerformed(e);
	}

	/**
	 * output actionExitCurrent_actionPerformed
	 */
	public void actionExitCurrent_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExitCurrent_actionPerformed(e);
	}

	/**
	 * output actionHelp_actionPerformed
	 */
	public void actionHelp_actionPerformed(ActionEvent e) throws Exception {
		super.actionHelp_actionPerformed(e);
	}

	/**
	 * output actionAbout_actionPerformed
	 */
	public void actionAbout_actionPerformed(ActionEvent e) throws Exception {
		super.actionAbout_actionPerformed(e);
	}

	/**
	 * output actionOnLoad_actionPerformed
	 */
	public void actionOnLoad_actionPerformed(ActionEvent e) throws Exception {
		super.actionOnLoad_actionPerformed(e);
	}

	/**
	 * output actionSendMessage_actionPerformed
	 */
	public void actionSendMessage_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionSendMessage_actionPerformed(e);
	}

	/**
	 * output actionCalculator_actionPerformed
	 */
	public void actionCalculator_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionCalculator_actionPerformed(e);
	}

	/**
	 * output actionExport_actionPerformed
	 */
	public void actionExport_actionPerformed(ActionEvent e) throws Exception {
		super.actionExport_actionPerformed(e);
	}

	/**
	 * output actionExportSelected_actionPerformed
	 */
	public void actionExportSelected_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExportSelected_actionPerformed(e);
	}

	/**
	 * output actionRegProduct_actionPerformed
	 */
	public void actionRegProduct_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionRegProduct_actionPerformed(e);
	}

	/**
	 * output actionPersonalSite_actionPerformed
	 */
	public void actionPersonalSite_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionPersonalSite_actionPerformed(e);
	}

	/**
	 * output actionProcductVal_actionPerformed
	 */
	public void actionProcductVal_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionProcductVal_actionPerformed(e);
	}

	/**
	 * output actionExportSave_actionPerformed
	 */
	public void actionExportSave_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExportSave_actionPerformed(e);
	}

	/**
	 * output actionExportSelectedSave_actionPerformed
	 */
	public void actionExportSelectedSave_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExportSelectedSave_actionPerformed(e);
	}

	/**
	 * output actionKnowStore_actionPerformed
	 */
	public void actionKnowStore_actionPerformed(ActionEvent e) throws Exception {
		super.actionKnowStore_actionPerformed(e);
	}

	/**
	 * output actionAnswer_actionPerformed
	 */
	public void actionAnswer_actionPerformed(ActionEvent e) throws Exception {
		super.actionAnswer_actionPerformed(e);
	}

	/**
	 * output actionRemoteAssist_actionPerformed
	 */
	public void actionRemoteAssist_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionRemoteAssist_actionPerformed(e);
	}

	/**
	 * output actionPopupCopy_actionPerformed
	 */
	public void actionPopupCopy_actionPerformed(ActionEvent e) throws Exception {
		super.actionPopupCopy_actionPerformed(e);
	}

	/**
	 * output actionHTMLForMail_actionPerformed
	 */
	public void actionHTMLForMail_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionHTMLForMail_actionPerformed(e);
	}

	/**
	 * output actionExcelForMail_actionPerformed
	 */
	public void actionExcelForMail_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExcelForMail_actionPerformed(e);
	}

	/**
	 * output actionHTMLForRpt_actionPerformed
	 */
	public void actionHTMLForRpt_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionHTMLForRpt_actionPerformed(e);
	}

	/**
	 * output actionExcelForRpt_actionPerformed
	 */
	public void actionExcelForRpt_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExcelForRpt_actionPerformed(e);
	}

	/**
	 * output actionLinkForRpt_actionPerformed
	 */
	public void actionLinkForRpt_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionLinkForRpt_actionPerformed(e);
	}

	/**
	 * output actionPopupPaste_actionPerformed
	 */
	public void actionPopupPaste_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionPopupPaste_actionPerformed(e);
	}

	@Override
	public void onLoad() throws Exception {
		super.onLoad();
		ActionListener[] ls = tblMain_detailPanel.getAddNewLineButton()
				.getActionListeners();
		for (int i = 0; i < ls.length; i++) {
			tblMain_detailPanel.getAddNewLineButton().removeActionListener(
					ls[i]);
		}
		tblMain_detailPanel.getAddNewLineButton().addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						try {
							AttendanceTimeConfigEditUI.this
									.actionAddLine_actionPerformed(e);
						} catch (Exception ex) {
							AttendanceTimeConfigEditUI.this.handleException(ex);
						}
					}
				});

		ls = tblMain_detailPanel.getInsertLineButton().getActionListeners();
		for (int i = 0; i < ls.length; i++) {
			tblMain_detailPanel.getInsertLineButton().removeActionListener(
					ls[i]);
		}
		tblMain_detailPanel.getInsertLineButton().addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						try {
							AttendanceTimeConfigEditUI.this
									.actionInsertLine_actionPerformed(e);
						} catch (Exception ex) {
							AttendanceTimeConfigEditUI.this.handleException(ex);
						}
					}
				});

		ls = tblMain_detailPanel.getRemoveLinesButton().getActionListeners();
		for (int i = 0; i < ls.length; i++) {
			tblMain_detailPanel.getRemoveLinesButton().removeActionListener(
					ls[i]);
		}

		tblMain_detailPanel.getRemoveLinesButton().addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						try {
							AttendanceTimeConfigEditUI.this
									.actionRemoveLine_actionPerformed(e);
						} catch (Exception ex) {
							AttendanceTimeConfigEditUI.this.handleException(ex);
						}
					}
				});
		loadFields();
	}

	public void actionAddLine_actionPerformed(ActionEvent e) throws Exception {
		addNewRow();
	}

	public void actionInsertLine_actionPerformed(ActionEvent e)
			throws Exception {
		insertRow();
	}

	public void actionRemoveLine_actionPerformed(ActionEvent e)
			throws Exception {
		deleteBlock();
	}

	private void addNewRow() {
		int tblRowOldCount = this.tblMain.getRowCount();

		IRow newRow = this.tblMain.addRow();
		newRow.getCell("number").getStyleAttributes().setBackground(
				REQUIREDBACKGROUND);
		newRow.getCell("name").getStyleAttributes().setBackground(
				REQUIREDBACKGROUND);
		newRow.getCell("beginDate").getStyleAttributes().setBackground(
				REQUIREDBACKGROUND);
		newRow.getCell("endDate").getStyleAttributes().setBackground(
				REQUIREDBACKGROUND);

		loadTableRow(createNewInfo(), newRow);

		ICell newRowCellOfBeginDate = newRow.getCell("beginDate");

		if (tblRowOldCount == 0) {
			if (newRow.getRowIndex() == 0) {
				newRowCellOfBeginDate.getStyleAttributes().setLocked(false);
				newRowCellOfBeginDate.getStyleAttributes().setBackground(
						REQUIREDBACKGROUND);
			}
		} else {
			int lastRowLineIndex = tblRowOldCount;

			int lastDataRowLineIndex = tblRowOldCount - 1;

			ICell lastCellOfEndDate = this.tblMain.getRow(lastDataRowLineIndex)
					.getCell("endDate");
			ICell lastCellOfBeginDate = this.tblMain.getRow(
					lastDataRowLineIndex).getCell("beginDate");

			Date newDateForBeginDate = null;
			if (lastCellOfEndDate.getValue() != null)
				newDateForBeginDate = DateTimeUtils.addDay(
						(Date) lastCellOfEndDate.getValue(), 1L);
			else if (lastCellOfBeginDate.getValue() != null)
				newDateForBeginDate = DateTimeUtils.addDay(
						(Date) lastCellOfBeginDate.getValue(), 1L);
			else {
				newDateForBeginDate = null;
			}

			if (tblRowOldCount == 0) {
				newRowCellOfBeginDate.getStyleAttributes().setLocked(false);
				newRowCellOfBeginDate.getStyleAttributes().setBackground(
						this.notEditCellBGColor);
			} else {
				newRowCellOfBeginDate.setValue(newDateForBeginDate);
			}
		}
	}

	private void insertRow() {
		KDTSelectBlock kb = this.tblMain.getSelectManager().get();
		if (kb != null) {
			int insertRowIndex = kb.getTop() + 1;
			IRow IRowTemp = this.tblMain.addRow(insertRowIndex);

			loadTableRow(createNewInfo(), IRowTemp);
		}
	}

	private void deleteBlock() throws Exception {
		boolean deleteFlag = true;

		int lastRowIndex = this.tblMain.getRowCount() - 1;

		if (lastRowIndex == -1) {
			return;
		}
		IRow iRow = this.tblMain.getRow(lastRowIndex);
		if ((iRow.getUserObject() instanceof AttendanceTimeConfigInfo)) {
			AttendanceTimeConfigInfo info = (AttendanceTimeConfigInfo) iRow
					.getUserObject();
			if (info.getId() != null) {
				deleteFlag = true;
			} else {
				deleteFlag = false;
			}
		}
		// if (!deleteFlag) {
		// throw new AttendanceTimeConfigException(
		// AttendanceTimeConfigException.NOT_DELETE);
		// }

		if (deleteFlag) {
			setHstSetCode(lastRowIndex);
		} else {
			this.tblMain.removeRow(lastRowIndex);
		}

		if (this.tblMain.getRowCount() > 0)
			this.rowID = (this.tblMain.getRowCount() - 1);
		else
			this.rowID = -1;
	}

	private void setHstSetCode(int lastRowIndex) {
		ICell cellDeleteNumber = this.tblMain.getRow(lastRowIndex).getCell(
				"number");
		this.tblMain.removeRow(lastRowIndex);
		if ((cellDeleteNumber.getValue() != null)
				&& (!this.hstCode.contains(cellDeleteNumber.getValue())))
			this.hstDeleteSet.add(cellDeleteNumber.getValue());
	}

	protected AttendanceTimeConfigInfo createNewInfo() {
		AttendanceTimeConfigInfo info = new AttendanceTimeConfigInfo();
		return info;
	}

	private void loadTableRow(AttendanceTimeConfigInfo info, IRow detailRow) {
		detailRow.setUserObject(info);

		detailRow.getCell("number").setValue(info.getNumber());
		detailRow.getCell("name").setValue(info.getName());
		detailRow.getCell("beginDate").setValue(info.getBeginDate());
		detailRow.getCell("endDate").setValue(info.getEndDate());
		detailRow.getCell("period").setValue(info.getPeriod());
		if (info.getId() != null) {
			detailRow.getCell("id").setValue(info.getId().toString());
			detailRow.getCell("number").getStyleAttributes().setLocked(true);
			detailRow.getCell("number").getStyleAttributes().setBackground(
					notEditCellBGColor);

			detailRow.getCell("beginDate").getStyleAttributes().setLocked(true);
			detailRow.getCell("beginDate").getStyleAttributes().setBackground(
					notEditCellBGColor);

			detailRow.getCell("endDate").getStyleAttributes().setLocked(true);
			detailRow.getCell("endDate").getStyleAttributes().setBackground(
					notEditCellBGColor);

			detailRow.getCell("period").getStyleAttributes().setLocked(true);
			detailRow.getCell("period").getStyleAttributes().setBackground(
					notEditCellBGColor);
		}
	}

	private IAttendanceTimeConfig getBizInterface() throws BOSException {
		return AttendanceTimeConfigFactory.getRemoteInstance();
	}

	@Override
	public void actionNaturalMonth_actionPerformed(ActionEvent e)
			throws Exception {
		verifyLastMonth();
		UIContext uiContext = new UIContext(this);

		uiContext.put("lastDate", getLastDate());

		IUIWindow uiWindowRefEdit = UIFactory.createUIFactory(
				UIFactoryName.MODEL).create(ImpNaturalMonthUI.class.getName(),
				uiContext);

		uiWindowRefEdit.show();

		ImpNaturalMonthUI naturalMonthUI = (ImpNaturalMonthUI) uiWindowRefEdit
				.getUIObject();
		HashMap hashDateMap = naturalMonthUI.getDateValue();
		if ((hashDateMap != null) && (hashDateMap.size() != 0)) {
			doNatureMonth(hashDateMap);
		}
	}

	@Override
	public void actionSave_actionPerformed(ActionEvent e) throws Exception {

		verifyInput();

		Hashtable hst = new Hashtable();
		HashSet cmpPeriodAddNewSet = new HashSet();
		HashSet cmpPeriodModifySet = new HashSet();
		HashSet cmpPeriodDeleteSet = new HashSet();
		AttendanceTimeConfigInfo info = null;
		AttendanceTimeConfigInfo configOPRInfo = new AttendanceTimeConfigInfo();
		String pkString = null;
		IObjectPK pk = null;

		int countCycle = this.tblMain.getRowCount();
		for (int i = 0; i < countCycle; i++) {
			this.row = this.tblMain.getRow(i);
			info = (AttendanceTimeConfigInfo) this.row.getUserObject();
			if (info.getId() == null) {
				cmpPeriodAddNewSet.add(info);
			} else {
				cmpPeriodModifySet.add(info);
				hst.put(info.getId(), info.getId());
			}
		}

		int countCycleOfColl = this.infoCollection.size();
		for (int j = 0; j < countCycleOfColl; j++) {
			info = this.infoCollection.get(j);
			Object obj = hst.get(info.getId());

			if (obj == null) {
				pk = new ObjectUuidPK(info.getId());
				pkString = pk.toString();
				cmpPeriodDeleteSet.add(pkString);
			}
		}

		if (!cmpPeriodDeleteSet.isEmpty()) {
			Object[] arrayCmpPeriodString = cmpPeriodDeleteSet.toArray();
			ObjectUuidPK[] pkArray = new ObjectUuidPK[arrayCmpPeriodString.length];

			int i = 0;
			for (int countCycleSize = arrayCmpPeriodString.length; i < countCycleSize; i++) {
				Object obj = arrayCmpPeriodString[i];
				pkArray[i] = new ObjectUuidPK(BOSUuid.read(obj.toString()));
			}

			getBizInterface().delete(pkArray);
		}

		if (!cmpPeriodAddNewSet.isEmpty()) {
			configOPRInfo.put("newCmpPeriodInfo", cmpPeriodAddNewSet);

			getBizInterface().addnew(configOPRInfo);
		}

		if (!cmpPeriodModifySet.isEmpty()) {
			configOPRInfo.put("modifyCmpPeriodInfo", cmpPeriodModifySet);
			getBizInterface().update(null, configOPRInfo);
		}

		this.hstCode.clear();
		this.hstDeleteSet.clear();

		this.isChang = false;

		this.loadFields();

	}

	private void doNatureMonth(HashMap hashDateMap) throws Exception {

		String year = hashDateMap.get("yearKey").toString();
		String beginMonth = hashDateMap.get("beginMonthKey").toString();
		String endMonth = hashDateMap.get("endMonthKey").toString();
		Boolean genNameFlag = (Boolean) hashDateMap.get("isGenName");
		int beginMonthInt = new Integer(beginMonth).intValue();
		int endMonthInt = new Integer(endMonth).intValue();
		int monthSize = endMonthInt + 1;
		IRow newRow = null;
		ICell beginDateCell = null;
		ICell endDateCell = null;
		ICell nameCell = null;
		ICell numberCell = null;
		ICell periodCell = null;
		String monthOfDateString = "";
		String endDateString = "";
		Date beginDate = null;
		Date endDate = null;
		CmpPeriodInfo period = null;
		for (int i = beginMonthInt; i < monthSize; i++) {
			monthOfDateString = year + "-" + (i - 1) + "-"
					+ resouce.getString("last_month");

			endDateString = year + "-" + i + "-"
					+ resouce.getString("next_month");

			endDate = HRUtils.stringConvertToDate("yyyy-MM-dd", endDateString);
			beginDate = HRUtils.stringConvertToDate("yyyy-MM-dd",
					monthOfDateString);

			addNewRow();
			newRow = this.tblMain.getRow(this.tblMain.getRowCount() - 1);
			beginDateCell = newRow.getCell("beginDate");
			endDateCell = newRow.getCell("endDate");
			beginDateCell.setValue(beginDate);
			endDateCell.setValue(endDate);

			nameCell = newRow.getCell("number");
			numberCell = newRow.getCell("name");
			periodCell = newRow.getCell("period");
			if (genNameFlag.booleanValue()) {
				if (i < 10) {
					nameCell.setValue(year + "0" + i);
					numberCell.setValue(year + "0" + i);
				} else {
					nameCell.setValue(year + i);
					numberCell.setValue(year + i);
				}
				period = this.getCmpPeriodInfo(nameCell.getValue().toString());

			}
			periodCell.setValue(period);
		}
		this.isChang = true;
	}

	public void verifyLastMonth() throws Exception {

		if (this.tblMain.getRowCount() != 0) {
			String lastDateString = getLastDate();
			int lastDateDay = new Integer(lastDateString.split("-")[2])
					.intValue();

			// String lastDateThisMonth = HRUtils
			// .getLastDayOfMonthOfYear(lastDateString);
			int lastDateThisMonthDay = new Integer(resouce
					.getString("next_month")).intValue();

			if (lastDateThisMonthDay != lastDateDay) {
				MsgBox
						.showWarning(EASResource
								.getString("com.kingdee.eas.hr.compensation.client.CmpPeriodResource."
										+ "CMPLETE_ENDDATE_2"));
				SysUtil.abort();
			}
		}
	}

	private String getLastDate() throws ParseException {
		String lastDate = null;
		int rowCount = this.tblMain.getRowCount();
		if (rowCount > 0) {
			rowCount--;
			IRow currRow = this.tblMain.getRow(rowCount);
			ICell currCell = currRow.getCell("endDate");
			if (currCell.getValue() != null) {
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				lastDate = formatter.format((Date) currCell.getValue());
			} else {
				MsgBox
						.showWarning(EASResource
								.getString("com.kingdee.eas.hr.compensation.client.CmpPeriodResource."
										+ "CMPLETE_ENDDATE"));
				SysUtil.abort();
			}
		}
		return lastDate;
	}

	public void loadFields() {
		super.loadFields();
		try {
			EntityViewInfo entityViewInfo = new EntityViewInfo();
			entityViewInfo.getSelector().add(new SelectorItemInfo("number"));
			entityViewInfo.getSelector().add(new SelectorItemInfo("name"));
			entityViewInfo.getSelector().add(new SelectorItemInfo("begindate"));
			entityViewInfo.getSelector().add(new SelectorItemInfo("enddate"));
			entityViewInfo.getSelector().add(new SelectorItemInfo("period.*"));

			SorterItemInfo sortInfo = new SorterItemInfo("begindate");
			sortInfo.setSortType(SortType.ASCEND);
			entityViewInfo.getSorter().add(sortInfo);

			this.infoCollection = getBizInterface()
					.getAttendanceTimeConfigCollection(entityViewInfo);
		} catch (Exception e) {
			handUIException(e);
		}

		this.tblMain.removeRows();

		int i = 0;
		for (int countCycle = this.infoCollection.size(); i < countCycle; i++) {
			AttendanceTimeConfigInfo info = this.infoCollection.get(i);
			IRow detailRow = this.tblMain.addRow();

			loadTableRow(info, detailRow);
			// unEnableEditor(detailRow.getRowIndex(), cmpPeriodInfo
			// .isEnableState());
		}

	}

	private HROrgUnitInfo getHROrgUnit() {
		if (this.hrOrgUnitInfo == null) {
			this.hrOrgUnitInfo = HRSchemeUtils.getClientHROrgUnitInfo();
		}
		return this.hrOrgUnitInfo;
	}

	private void verifyInput() throws Exception {
		int i = 0;
		AttendanceTimeConfigInfo info = null;
		for (int countCycle = this.tblMain.getRowCount(); i < countCycle; i++) {
			this.row = this.tblMain.getRow(i);
			info = (AttendanceTimeConfigInfo) this.row.getUserObject();

			Object obj = this.row.getCell("number").getValue();
			if (obj == null) {
				throw new AttendanceTimeConfigException(
						AttendanceTimeConfigException.CODE_NOT_EMP);
			}

			if (this.hstCode.get(obj) != null) {
				if (!this.hstDeleteSet.contains(obj)) {
					throw new AttendanceTimeConfigException(
							AttendanceTimeConfigException.EXIST_CODE);
				}
			}

			info.setNumber(obj.toString());
			this.hstCode.put(obj, obj);

			obj = this.row.getCell("name").getValue();
			if (obj == null) {
				throw new AttendanceTimeConfigException(
						AttendanceTimeConfigException.NAME_NOT_EMP);
			}
			info.setName(obj.toString());

			obj = this.row.getCell("beginDate").getValue();
			if (obj == null) {
				throw new AttendanceTimeConfigException(
						AttendanceTimeConfigException.BEGINDATE_NOT_EMP);
			}
			info.setBeginDate((Date) obj);

			obj = this.row.getCell("endDate").getValue();
			if (obj == null) {
				throw new AttendanceTimeConfigException(
						AttendanceTimeConfigException.ENDDATE_NOT_EMP);
			}
			info.setEndDate((Date) obj);

			obj = this.row.getCell("period").getValue();
			if (obj == null) {
				throw new AttendanceTimeConfigException(
						AttendanceTimeConfigException.PERIOD_NOT_NULL);
			}
			info.setPeriod((CmpPeriodInfo) obj);
		}
	}

	private CmpPeriodInfo getCmpPeriodInfo(String number) throws Exception {

		CmpPeriodCollection collection = CmpPeriodFactory.getRemoteInstance()
				.getCmpPeriodCollection("where number='" + number + "'");

		if (collection.isEmpty()) {
			return null;
		}
		return collection.get(0);
	}
}